<?php
/**
 * Created by PhpStorm.
 * User: zgh
 * Date: 2022-01-20
 * Time: 16:04
 */

namespace app\common\pipeline;

use think\facade\Hook;

class BaseItemAdapter
{
    protected $field = "*";

    protected $className = "";

    protected $model = null;

    protected $primary_key = "id"; //主键
    protected $hook_arr = [];
    protected $auto_exec_hook = true; //保存之后是否自动运行钩子

    public function hookAdd($name, $params)
    {
        $this->hook_arr[$name] = $params;
        return $this;
    }

    public function hookExec()
    {
        $data = [];
        if (!empty($this->model)) {
            $data = $this->model->toArray();
        }
        foreach ($this->hook_arr as $key => $params) {
            if (is_array($params)) {
                $p = [];
                foreach ($params as $index => $value) {
                    if (is_string($value) && substr($value, 0, 1) == '?') {
                        $field = substr($value, 1);
                        $p[$index] = isset($data[$field]) ? $data[$field] : "";
                    } else {
                        $p[$index] = $value;
                    }
                }
                Hook::listen($key, $p);
            } else {
                Hook::listen($key, $params);
            }
        }
    }


    public function __construct($className = "", $autoExecHook = "")
    {
        if (!empty($className)) {
            $this->setClass($className);
        }
        if ($autoExecHook !== "") {
            $this->auto_exec_hook = $autoExecHook;
        }
    }

    public function setClass($class_name)
    {
        $this->className = $class_name;
        return $this;
    }

    public function create()
    {
        $reflect = new \ReflectionClass($this->className);
        $model = $reflect->newInstanceArgs();
        $this->model = $model;
        return $this;
    }

    public function get($id)
    {
        $model = ($this->className)::get($id);
        $this->model = $model;
        return $this;
    }

    public function where($where, $order = "")
    {
        $query = ($this->className)::where($where);
        if (!empty($order)) {
            $query = $query->order($order);
        }
        $model = $query->find();
        $this->model = $model;
        return $this;
    }

    public function id($id)
    {
        if ($id > 0) {
            $this->get($id);
        } else {
            $this->create();
        }
        return $this;
    }


    public function save($data)
    {
        $this->emptyModel($data);
        $res = $this->model->save($data);
        if ($this->auto_exec_hook) {
            $this->hookExec();
        }
        return $res;
    }

    protected function emptyModel($data)
    {
        if (empty($this->model)) {
            $id = isset($data[$this->primary_key]) ? $data[$this->primary_key] : 0;
            $this->id($id);
        }
        return $this;
    }

    public function getModel()
    {
        return $this->model;
    }

    public function getId($key = "id")
    {
        return $this->model->$key;
    }

    public function whereUpdate($where, $update)
    {
        $res = ($this->className)::where($where)->update($update);
        if ($this->auto_exec_hook) {
            $this->hookExec();
        }
        return $res;
    }

    public function whereDel($where)
    {
        $res = ($this->className)::where($where)->delete();
        if ($this->auto_exec_hook) {
            $this->hookExec();
        }
        return $res;
    }


    public function saveAll($data)
    {
        $this->create();
        $this->model->saveAll($data);
    }

    public function checkModel()
    {
        return (bool)$this->model;
    }

    public function getData()
    {
        $data = [];
        if ($this->model) {
            $data = $this->model->getData();
        }
        return $data;
    }
}